package report

import (
	"gorm.io/gorm"
	"xkginweb/global"
	"xkginweb/model/entity/report"
	"xkginweb/model/entity/report/request"
)

// 定义report的service提供report的数据curd的操作

type ReportService struct{}

// @author: hcl
// @function: CreateReport
// @description: 创建上报记录
// @param: report.QccReport
// @return: err error

func (service *ReportService) CreateReport(report *report.QccReport) (err error) {
	// 1： 获取数据的连接对象 如果执行成功err是nil，如果失败就把失败告诉
	err = global.KSD_DB.Create(report).Error
	return err
}

// @author: hcl
// @function: GetRoom
// @description: 根据ID获取房间信息
// @param: id uint
// @return: room *room.QccRoom, err error

func (service *ReportService) GetReport(id uint) (report *report.QccReport, err error) {
	err = global.KSD_DB.Where("report_id = ?", id).Preload("QccRoom", func(db *gorm.DB) *gorm.DB {
		return db.Table("qcc_room").Select("room_id,room_name,room_type")
	}).Preload("QccReporter", func(db *gorm.DB) *gorm.DB {
		return db.Table("qcc_reporter").Select("reporter_id,username,name,sex,phone_number,address,type")
	}).First(&report).Error
	return
}

//@author: hcl
//@function: UpdateReporter
//@description: 更新上报记录
//@param: reporter.QccReporter
//@return: err error

func (service *ReportService) UpdateReport(report *report.QccReport) (err error) {
	//err = global.KSD_DB.Save(reporter).Error
	err = global.KSD_DB.Model(report).Updates(report).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: LoadReportPage
//@description: 分页获取上报数据列表
//@param: sysUserAuthorityID string, info request.PageInfo
//@return: list interface{}, total int64, err error

func (service *ReportService) LoadReportPage(info request.ReportPageInfo) (list interface{}, total int64, err error) {
	// 获取分页的参数信息
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)

	// 准备查询那个数据库表
	db := global.KSD_DB.Model(&report.QccReport{})

	// 准备切片帖子数组
	var ImportantDeviceList []report.QccReport

	// 加条件
	//if info.Keyword != "" {
	//	db = db.Where("(cooling_pool_name like ?  )", "%"+info.Keyword+"%")
	//}

	if info.RoomId != 0 {
		db = db.Where("(room_id = ?  )", info.RoomId)
	}

	if info.ReporterId != 0 {
		db = db.Where("(reporter_id = ?  )", info.ReporterId)
	}
	if info.StartTime != "" {
		db = db.Where("(created_at >= ?  )", info.StartTime)
	}
	if info.EndTime != "" {
		db = db.Where("(created_at <= ?  )", info.EndTime)
	}
	if info.Status != "" {
		db = db.Where("(status = ?  )", info.Status)
	}

	// 排序默时间降序降序
	db = db.Order("created_at")

	// 查询中枢
	err = db.Count(&total).Error
	if err != nil {
		return ImportantDeviceList, total, err
	} else {
		// 执行查询
		err = db.Limit(limit).Offset(offset).Omit("detail").Preload("QccRoom", func(db *gorm.DB) *gorm.DB {
			return db.Table("qcc_room").Select("room_id,room_name,room_type")
		}).Preload("QccReporter", func(db *gorm.DB) *gorm.DB {
			return db.Table("qcc_reporter").Select("reporter_id,username,name,sex,phone_number,address,type")
		}).Find(&ImportantDeviceList).Error
	}

	// 结果返回
	return ImportantDeviceList, total, err
}
